<?php

namespace Core\Common\Base;

use Core\Common\Base\Db\DataAccessor;

/**
 * 使用方法
 * 1.
 * class UserData extends \Core\Common\Base\Db\Database
   {
    const DB_NAME = 'sqkb';
    const TABLE_NAME = 'user';
   }
 * UserData::getData(['id' => 1]);
 *
 * 2.
use Core\Common\Model\BaseModel;

class UserModel extends BaseModel
{
    protected $table = 'user';
    protected $connection = 'sqkb';
    protected $primaryKey = 'id';

    public function info($id)
    {
        if (empty($id)) {
            return [];
        }

        $info = $this->where($this->primaryKey, $id)->first();
        if (empty($info)) {
            return [];
        }

        $info = $info->toArray();
        return $info;
    }
}
 * Model基类
 * @author baiyang@ibantang.com
 */
class Model
{
    protected $connection = '';
    protected $table = '';
    protected $primaryKey = 'id';
    /**
     *
     * @var DataAccessor
     */
    protected $dataAccessor = null;
    
    public function __construct()
    {
        $this->dataAccessor = new DataAccessor($this->connection, $this->table, $this->primaryKey);
    }
    
    public function __call($name, $arguments)
    {
        return $this->dataAccessor->$name(...$arguments);
    }

    public static function __callStatic($name, $arguments)
    {
        $object = new static();
        if (!method_exists($object->dataAccessor, $name)) {
            throw new \Exception('no method for: ' . __CLASS__."::$name");
        }
        $object->dataAccessor->$name(...$arguments);
        return $object;
    }
}
